<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace manniu\controller;

use think\Config;
use think\Cache;

class AdminBase extends Base{
	/**
	 * 初始化
	 */
    public function _initialize(){
    	parent::_initialize();
    	// 判断后台入口标识，不存在则跳转到首页
    	if(!defined('IS_ADMIN')){
    		$this->redirect($this->request->domain());
    	}
    	// 后台初始化
    	$this->adminInit();
    }

    /**
	 * 后台初始化
	 */
	protected function adminInit(){
		// 验证管理员登录状态
		if($this->checkLogin()){
			// 初始化验证类配置信息
            Config::set([
			    'mn_auth_config' => [
			    	'auth_group'        => 'admin_role', 		// 用户组数据表名
			        'auth_rule'         => 'admin_auth_rule', 	// 权限规则表
			        'auth_user'         => 'admin', 			// 用户信息表
			        'id_fieldname'      => 'aid' 				// 用户信息表，用户id字段名
			    ],
			]);
			// 更新后台关键缓存
			$this->adminCache();
			// 验证管理员权限
			$this->checkAuth();
			// 把公共信息赋值给模板
			$this->assign('admin', $this->admin);
			$this->assign('menus', $this->getMenuList());
			// 后台模版布局
			$this->view->engine->layout('admin_layout/base');
		}
	}

	/**
	 * 验证管理员登录状态
	 */
	protected function checkLogin(){
		if($this->aid){
			return true;
		}
		if(strtolower($this->request->controller()) != 'login'){
			$this->redirect('admin/login/index');
		}
		return false;
	}

	/**
	 * 验证管理员权限
	 */
	protected function checkAuth(){
        $admin_auth = new Auth();
		$checkname = strtolower(str_replace('.', '-', $this->request->module().'-'.$this->request->controller().'-'.$this->request->action()));
		if(!$admin_auth->check($checkname,$this->aid)){
			$this->error('抱歉，无操作权限！');
		}
	}

	/**
	 * 获取菜单
	 * @return array | bool
	 */
	protected function getMenuList(){
		$data = Cache::get('admin_menu');
		// 过滤没有操作权限的菜单
		if((int)$this->admin['role_id'] != 1){
			foreach ($data as $t_key => $top) {
				foreach ($top['left'] as $l_key => $left) {
					// 过滤链接菜单
					foreach ($left['link'] as $link_key => $link) {
						$checkname = str_replace(['/','.'], '-', strtolower($link['uri']));
                        $admin_auth = new Auth();
						if(!$admin_auth->check($checkname,$this->aid)){
							unset($data[$t_key]['left'][$l_key]['link'][$link_key]);
						}
					}
					// 过滤分组菜单
					if(empty($data[$t_key]['left'][$l_key]['link'])){
						unset($data[$t_key]['left'][$l_key]);
					}
				}
				// 过滤顶级菜单
				if(empty($data[$t_key]['left'])){
					unset($data[$t_key]);
				}
			}
		}
		if($data){
			return $data;
		}else{
			return NULL;
		}
	}

	/**
     * 获取栏目与单页树形结构 - select
     * @return	string
     */
    protected function getCategorys() {
    	//所有模块信息
		$data = '';
		return $data;
    }

	/**
	 * 更新后台关键缓存
	 */
	protected function adminCache() {
		//后台菜单
		if(!Cache::get('admin_menu')){
			$AdminMenu = new \app\admin\model\AdminMenu;
			$AdminMenu->cache();
		}
		//管理员角色组
		if(!Cache::get('admin_role')){
			$AdminRole = new \app\admin\model\AdminRole;
			$AdminRole->cache();
		}
	}
}
